﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class DiscountOptionObject
    {
        public static DiscountOption[] GetDiscountOptions()
        {
            return Query(null);
        }

        public static DiscountOption GetDiscountOption(int id)
        {
            DiscountOption discountOption = new DiscountOption();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(DiscountOptionDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = DiscountOptionDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable discountOptions = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            discountOption.Id               = Convert.ToInt32(discountOptions.Rows[0][DiscountOptionDesc.Id]);
            discountOption.Name             = Convert.ToString(discountOptions.Rows[0][DiscountOptionDesc.Name]);
            discountOption.Discount         = Convert.ToInt32(discountOptions.Rows[0][DiscountOptionDesc.Discount]);

            return discountOption;
        }

        public static DiscountOption GetDiscountOption(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(DiscountOptionDesc.Name, ConditionExpress.EQUALS, name);

            DiscountOption[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static DiscountOption[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + DiscountOptionDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                DiscountOption[] discountOptions = new DiscountOption[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DiscountOption discountOption = new DiscountOption();

                    discountOption.Id          = Convert.ToInt32(table.Rows[i][DiscountOptionDesc.Id]);
                    discountOption.Name        = Convert.ToString(table.Rows[i][DiscountOptionDesc.Name]);
                    discountOption.Discount    = Convert.ToInt32(table.Rows[i][DiscountOptionDesc.Discount]);

                    discountOptions.SetValue(discountOption, i);
                }

                return discountOptions;
            }
            return null;
        }

        public static Int32 Add(DiscountOption discountOption)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(DiscountOptionDesc.Name,          discountOption.Name);
            builder.InsertAdd(DiscountOptionDesc.Discount,      discountOption.Discount);
            builder.QueryTableName = DiscountOptionDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(DiscountOptionDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = DiscountOptionDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(DiscountOption discountOption)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(DiscountOptionDesc.Name,             discountOption.Name);
            builder.UpdateAdd(DiscountOptionDesc.Discount,         discountOption.Discount);
     
            builder.ConditionAdd(DiscountOptionDesc.Id, ConditionExpress.EQUALS, discountOption.Id);
            builder.QueryTableName = DiscountOptionDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
